package com.threeti.mecool.framework.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.UrlPathHelper;

/**
 * @author Jay Meng 
 */
public class CommonWebUtils {
  public static final String AJAX_JQUERY_HEADER_KEY = "x-requested-with";
  public static final String AJAX_JQUERY_HEADER_VALUE = "XMLHttpRequest";

  public static final String ORIGINATING_DECODED_URL = "originatingDecodedURL";
  public static final String LOOKUP_PATH_FOR_REQUEST = "lookupPathForRequest";
  public static final String ORIGINATING_QUERY_STRING = "originatingQueryString";

  /**
   * 取得引渡过来的URL：
   */
  public static String getOriginatingDecodedURL(HttpServletRequest request) {
    return getOriginatingDecodedURL(request, false, false);
  }

  /**
   * 取得引渡过来的URL：
   */
  public static String getOriginatingDecodedURL(HttpServletRequest request, boolean isRemoveContextPath) {
    String uri = request.getParameter(ORIGINATING_DECODED_URL);
    if (uri != null) {
      if (isRemoveContextPath) uri = uri.replaceFirst(request.getContextPath()+"/", "");//remove context path prefix
      return uri;
    }
    UrlPathHelper urlHelper = new UrlPathHelper();
    uri = urlHelper.getOriginatingRequestUri(request);
    String query = urlHelper.getOriginatingQueryString(request);

    if (isRemoveContextPath) uri = uri.replaceFirst(request.getContextPath()+"/", "");//remove context path prefix

    if (query != null) {
      return uri + "?" + query;
    }

    return uri;
  }

  /**
   * 取得引渡过来的URL：
   */
  public static String getOriginatingDecodedURL(HttpServletRequest request, boolean isRemoveContextPath, boolean isRemoveQueryString) {
    String uri = request.getParameter(ORIGINATING_DECODED_URL);
    if (uri != null) {
      if (isRemoveContextPath) uri = uri.replaceFirst(request.getContextPath()+"/", "");//remove context path prefix
      return uri;
    }
    UrlPathHelper urlHelper = new UrlPathHelper();
    uri = urlHelper.getOriginatingRequestUri(request);
    String query = urlHelper.getOriginatingQueryString(request);

    if (isRemoveContextPath) uri = uri.replaceFirst(request.getContextPath()+"/", "");//remove context path prefix

    if (StringUtils.isNotBlank(query) && !isRemoveQueryString) {
      return uri + "?" + query;
    }

    return uri;
  }

  /**
   * 取得引渡过来的URL PARAM
   */
  public static String getOriginatingQueryString(HttpServletRequest request) {
    return new UrlPathHelper().getOriginatingQueryString(request);
  }

  /**
   * 取得正欲跳转前往的URL：
   */
  public static String getLookupPathForRequest(HttpServletRequest request, boolean isRemoveContextPath) {
    String uri = request.getParameter(LOOKUP_PATH_FOR_REQUEST);
    if (uri != null) {
      if (isRemoveContextPath) uri = uri.replaceFirst(request.getContextPath()+"/", "");//remove context path prefix
      return uri;
    }

    UrlPathHelper urlHelper = new UrlPathHelper();
    uri = urlHelper.getLookupPathForRequest(request);

    String contextPath = urlHelper.getContextPath(request);
    if (isRemoveContextPath) uri = uri.replaceFirst(contextPath+"/", "");//remove context path prefix

    return uri;
  }

  /** 本方法目前仅支持JQuery发起的Ajax请求 */
  public static boolean isAjaxRequest(HttpServletRequest request) {
    String header = request.getHeader(AJAX_JQUERY_HEADER_KEY);
    return (header != null && header.equals(AJAX_JQUERY_HEADER_VALUE));
  }
}
